package com.fyzk.activity.user.redpacket;

import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.fyzk.bean.RedPacketReceive;
import com.tyjc.lottery.R;
import com.fyzk.net.Commands;
import com.fyzk.net.PublicMethod;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;

import http.Common.CommandUtil;
import http.StringResultBean;

/**
 * Created by miao on 2017/4/26.
 */
@ContentView(R.layout.fg_redpacket)
public class ReceiveRedPacketFragment extends RedpacketFragment {

    int page = 1;


    @ViewInject(R.id.redpacket_list)
    ListView redpacket_list;
    @ViewInject(R.id.null_record)
    TextView null_record;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        return x.view().inject(this, inflater, container);//xutil注解;
    }

    @Override
    public void onResume() {
        super.onResume();
        requestReceiveRedpacketRecord(page);

    }

    ArrayList<RedPacketReceive> redPacketReceives;

    private void requestReceiveRedpacketRecord(int page) {
        try {
            JSONObject request_json = new JSONObject();
            request_json.put("pageIndex", page + "");
            request_json.put("maxResult", 50 + "");
            httpPostJson(CommandUtil.FINDUSERRECEIVEREDPACKAGE, Commands
                    .FINDUSERRECEIVEREDPACKAGE, request_json, false);
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    private void requestReceiveRedpacket(String redPackageId) {
        try {
            JSONObject request_json = new JSONObject();
            request_json.put("redPackageId", redPackageId);
            httpPostJson(CommandUtil.RECEIVEPACKAGE, Commands.RECEIVEPACKAGE, request_json, false);
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }


    class RedPacketAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return redPacketReceives.size();
        }

        @Override
        public RedPacketReceive getItem(int position) {
            return redPacketReceives.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(getContext(), R.layout.item_redpacketreceive, null);
                holder.amount = (TextView) convertView.findViewById(R.id.amount);
                holder.phone = (TextView) convertView.findViewById(R.id.phonenum);
                holder.date = (TextView) convertView.findViewById(R.id.date);
                holder.recevice = (Button) convertView.findViewById(R.id.btn_receiveredpacket);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            RedPacketReceive redPacketReceive = getItem(position);
            String redpacket = "红包彩金：<font color=\"#ef3338\">" + PublicMethod.formatMoney
                    (redPacketReceive.getAmount() / 1d) + "米</font>";
            holder.amount.setText(Html.fromHtml(redpacket));
            holder.date.setText(redPacketReceive.getReceiveTime());
            holder.phone.setText(redPacketReceive.getReceivePhone());
//            status	String	红包状态 1领取成功 2过期 3未领取
            holder.recevice.setTag(redPacketReceive);
            switch (redPacketReceive.getStatus()) {
                case 1:
                    holder.recevice.setText("已领取");
                    holder.recevice.setEnabled(false);
                    break;
                case 2:
                    holder.recevice.setText("已过期");
                    holder.recevice.setEnabled(false);
                    break;
                case 3:
                    holder.recevice.setText("领取");
                    holder.recevice.setEnabled(true);
                    break;
            }
            holder.recevice.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    RedPacketReceive redPacketReceive = (RedPacketReceive) v.getTag();
                    redpacketId = redPacketReceive.getId();
                    requestReceiveRedpacket(redPacketReceive.getId());
                }
            });

            return convertView;
        }


        class ViewHolder {
            TextView phone;
            TextView date;
            TextView amount;
            Button recevice;
        }
    }

    String redpacketId = "";

    RedPacketAdapter redPacketAdapter = null;

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        super.onSuccess(requestCode, resultBean, result);

        switch (requestCode) {
            case CommandUtil.RECEIVEPACKAGE:
                RedPacketReceive  redpacket = new RedPacketReceive();
                redpacket.setId(redpacketId);
                int index = redPacketReceives.indexOf(redpacket);
                redPacketReceives.get(index).setStatus(1);
                redPacketAdapter.notifyDataSetChanged();
                showToast("领取成功");
                break;
            case CommandUtil.FINDUSERRECEIVEREDPACKAGE:
                if (resultBean.getErrorcode().equals("0")) {
                    try {
                        JSONObject jsonObject = new JSONObject(resultBean.getData());
                        redPacketReceives = new ArrayList<>();
                        redPacketReceives.addAll(JSON.parseArray(jsonObject.getString
                                ("redPackages"), RedPacketReceive.class));
                        redpacket_list.setAdapter(new RedPacketAdapter());
                        if (redPacketReceives.size() > 0) {
                            redPacketAdapter = new RedPacketAdapter();
                            redpacket_list.setAdapter(redPacketAdapter);
                            null_record.setVisibility(View.GONE);
                        } else {
                            null_record.setVisibility(View.VISIBLE);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                break;
        }


    }


    @Override
    public void onFailure(int requestCode, String errorcode, String msg) {
//        showToast(msg);
        if(!errorcode.equals("900003")){
            showToast(msg);
        }
    }
}
